/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.jinyao.exploit.modules.gen.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jinyao.exploit.common.base.web.BaseController;
import com.jinyao.exploit.common.base.User;
import com.jinyao.exploit.common.utils.StringUtils;
import com.jinyao.exploit.common.utils.UserUtils;
import com.jinyao.exploit.modules.gen.entity.Template;
import com.jinyao.exploit.modules.gen.service.TemplateService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 代码模板Controller
 * @author ThinkGem
 * @version 2013-10-15
 */
@Controller
@RequestMapping(value = "${baseAdminPath}/gen/template")
public class TemplateController extends BaseController {

	@Resource(name = "templateServiceImpl")
	private TemplateService templateService;
	
	@ModelAttribute
	public Template get(@RequestParam(required=false) long id) {
		if (id != 0){
			return templateService.getById(id);
		}else{
			return new Template();
		}
	}
	
	@RequestMapping(value = {"list", ""})
	public String list(Template template, HttpServletRequest request, HttpServletResponse response, Model model) {
        IPage<Template> page = templateService.page(new Page<>());
        model.addAttribute("page", page);
		return "modules/gen/templateList";
	}

	@RequestMapping(value = "form")
	public String form(Template template, Model model) {
		model.addAttribute("template", template);
		return "modules/gen/templateForm";
	}

	@RequestMapping(value = "save")
	public String save(Template template, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, template)){
			return form(template, model);
		}
		templateService.save(template);
		addMessage(redirectAttributes, "保存代码模板'" + template.getName() + "'成功");
		return "redirect:" + adminPath + "/gen/template/?repage";
	}
	
	@RequestMapping(value = "delete")
	public String delete(Template template, RedirectAttributes redirectAttributes) {
		templateService.removeById(template);
		addMessage(redirectAttributes, "删除代码模板成功");
		return "redirect:" + adminPath + "/gen/template/?repage";
	}

}
